//
//  ZPhoneVerifyVC.m
//  WeClubs
//
//  Created by Zed Link on 28/3/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZPhoneVerifyVC.h"
#import "ZCountDownButton.h"
#import "ZPasswordVC.h"
#import "ZUserInfo.h"
#import "ZTabbarController.h"
#import <SMS_SDK/SMSSDK.h>


@interface ZPhoneVerifyVC ()

@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UITextField *passwordField;
@property (weak, nonatomic) IBOutlet UITextField *verifyField;

@property (weak, nonatomic) IBOutlet ZCountDownButton *verifyBtn;
@property (weak, nonatomic) IBOutlet UIButton *nextStepBtn;

@end

@implementation ZPhoneVerifyVC

+ (instancetype)zNew
{
    return [self zStoryboardInstanceWithStoryboardName:@"LoginAndRegister"];
}

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"新用户注册";
    [self.navSeparatorLine setBackgroundColor:[UIColor zColorWithHex:0xf3f5f7]];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.view setBackgroundColor:LOGIN_VIEW_BG_COLOR];

    [_verifyBtn setVerifyStateTitle:@"获取验证码" andColor:[UIColor zColorWithHex:0x2fcafb]];
    [_verifyBtn setCountDownStateTitle:@"重新获取" andColor:[UIColor zColorWithHex:0x999999]];
    
    // 输入验证码才能下一步
    self.nextStepBtn.enabled = NO;
    [self.phoneField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.passwordField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.verifyField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == self.phoneField && textField.text.length > 11)
    {
        textField.text = [textField.text substringToIndex:11];
    }
    
    if (self.phoneField.text.length > 0 && self.passwordField.text.length > 0 && self.verifyField.text.length > 0) {
        self.nextStepBtn.enabled = YES;
    } else {
        self.nextStepBtn.enabled = NO;
    }
}

#pragma mark - Control Action

// 获取验证码
- (void)verifyBtnAction:(id)sender
{
    NSString *phoneNumber = _phoneField.text;
    
    // Tips: 手机号码错误提示
    if ([phoneNumber zCheckPhoneNumber] == NO) {
        [MBProgressHUD zAlertToastMessage:kPhoneError];
        return;
    }

    // Todo: 自定义短信模板标识customIdentifier, 审核通过后从官网http://www.mob.com上申请
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:phoneNumber zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (error) {
            ZLog(@"申请短信验证码失败: %@", error);
        } else {
            ZLog(@"短信验证码申请成功");
        }
    }];

    // 进入倒计时
    [self.verifyBtn beginCountDownWithTime:60];
}

// 完成注册
- (void)commitBtnAction:(id)sender
{
    // 判断是否符合规则
    if(_passwordField.text.length < 6 || _passwordField.text.length > 20) {
        
        [MBProgressHUD zAlertMessage:kPasswordError];
        return;
    }
    
    if (_verifyField.text.length != 4) {
        
        [MBProgressHUD zAlertMessage:@"验证码输入有误"];
        return;
    }
    
    if ([_phoneField.text zCheckPhoneNumber] == NO) {
        [MBProgressHUD zAlertToastMessage:kPhoneError];
        return;
    }
    
    NSDictionary *param = @{
                               @"mobile" : _phoneField.text,
                               @"code" : _verifyField.text,
                               @"password" : [_passwordField.text  zMD5HexString],
                               };

    [MBProgressHUD zAlertProgress:@"正在注册,请稍后"];
    [HTTPRequest POST:kRegisterURL parameter:param success:^(id resposeObject) {
        
        [MBProgressHUD zAlertHideHUD];
        [MBProgressHUD zAlertSuccess:@"注册成功"];
        
        ZLog(@"%@",resposeObject);
    } failure:^(NSError *error) {
        ZLog(@"%@",error);
         [MBProgressHUD zAlertHideHUD];
        NSString *errorMsg = error.userInfo[@"message"];
        [MBProgressHUD zAlertError:@""];
    }];
    
//    [[ZNetManager sharedInstance] registerNewUserWithPassword:_passwordField.text phoneNumber:_phoneField.text verifyCode:_verifyField.text success:^(ZNetManagerStatus status, NSString *resultMsg, NSDictionary *data)
//    {
//        // 验证成功, 保存数据, 跳转到密码设置
//        [ZUserInfo sharedInstance].userId = [data[@"user_id"] longValue];
//        [ZUserInfo sharedInstance].token = data[@"token"];
//        
//        ZTabbarController *tabVC = [[ZTabbarController alloc] init];
//        [self.view.window setRootViewController:tabVC];
//    } failure:^(NSString *resultMsg, NSDictionary *data)
//    {
//        [MBProgressHUD zAlertToastMessage:@"验证码不正确!"];
//    }];
}

@end
